subroutine mcmoves(invtemp,dmc_c)
use type
use var
implicit none
integer :: i, j, k, x, label=1, nclus
real(dp) :: rand1, old_energy, new_energy, ghost_energy, invtemp, dE, dmc_c
real(dp) ::  LdG_Energy, elastic_energy, eee
logical :: selection, flag
real(dp), dimension(3) :: rand
real(dp), dimension(6) :: q_bk, q_sel

Do x = 1, nodes
   
   ! Selection
   selection = .false.
   do while (selection == .false.) 
         call random_number(rand1)
         nclus  =  int(5*rand1) + 1
         call random_number(rand)
         i = int(dime(1)*rand(1)) + 1 
         j = int(dime(2)*rand(2)) + 1 
         k = int(dime(3)*rand(3)) + 1 
         if (boundary(i,j,k) == .false..and.NP(i,j,k) == .false.) then
            flag = .false.
            label = 1 + (i-1) + (j-1)*dime(1) + (k-1)*dime(1)*dime(2)
            q_sel = q(:,label)
            call random_number(rand1)
            q_sel(nclus) = q_sel(nclus) + dmc_c*(rand1 - 0.5d0)
            q_sel(6) = -q_sel(1)-q_sel(4)
!            call check_bound(q_sel, flag )
!            if (flag==.true.) then ! Selection is accepted
                selection = .true.
!            endif    
         endif
   enddo
   
   old_energy = genergy(label) + Elastic_Energy(label)
   q_bk = q(:,label)
   q(:,label) = q_sel
   eee = LdG_Energy(q(:,label))
   new_energy = eee + Elastic_Energy(label)

   dE = (new_energy - old_energy) * invtemp
   call random_number(rand1)
   if (dE > 0.d0.and. rand1 > exp(-dE)) then
      q(:,label) = q_bk
   else
      genergy(label) = eee
   endif

enddo

end subroutine mcmoves       


   
